package activiti2;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

/**
 *
 * 接收任务（ReceiveTask）
 *
 */
public class JieShouRenWu {

    //流程引擎对象
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署   发布
     */
    @Test
    public void deployementAndStartProcess(){
        //部署
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("diagrams/jieshourenwu.bpmn")
                .deploy();
        System.out.println("部署ID："+deployment.getId());

        //启动流程
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey("myProcess_6");
        System.out.println("流程实例ID："+pi.getId());
        System.out.println("流程定义ID："+pi.getProcessDefinitionId());

        //3.查询执行对象表,使用流程实例ID和当前活动的名称（receivetask1）
        String processInstanceId = pi.getId();

        Execution execution1 = processEngine.getRuntimeService()
                .createExecutionQuery()
                .processInstanceId(processInstanceId)
                .activityId("_2")
                .singleResult();

        //4.使用流程变量设置当日的销售额
        processEngine.getRuntimeService().setVariable(execution1.getId(),"当日销售额",20000);
        //5.向后执行一步
        processEngine.getRuntimeService().signal(execution1.getId());
        //6.查询执行对象表,使用流程实例ID和当前活动的名称（receivetask2）
        Execution execution2 = processEngine.getRuntimeService()
                .createExecutionQuery()
                .processDefinitionId(processInstanceId)
                .activityId("_2")
                .singleResult();
        //7.获取流程变量,给老板发送短信
        Integer value = (Integer) processEngine.getRuntimeService().getVariable(execution2.getId(),"当日销售额");
        System.out.println("给老板发送短信：内容，当日销售额："+value);

        //8.向后执行一步
        processEngine.getRuntimeService()
                .signal(execution2.getId());
        //9.判断流程是否结束
        ProcessInstance nowPi = processEngine.getRuntimeService()
                .createProcessInstanceQuery()
                .processInstanceId(pi.getId())
                .singleResult();
        if(nowPi == null){
            System.out.println("流程结束");
        }



    }





}
